// 1. 解析 payload
var payloadObj = JSON.parse(msg.payload);

// 2. 提取 topic 中的Code
var topicParts = msg.topic.split('/');
var method = topicParts[topicParts.length - 1]; // telemetry、tel
var deviceCode = topicParts[topicParts.length - 2]; // dtu01
var systemCode = topicParts[topicParts.length - 3];
var siteCode = topicParts[topicParts.length - 4];

if (['telemetry', 'tel'].indexOf(method) < 0) {
  return;
}

var systemParts = systemCode.split('-');
var workshopNo = systemParts[0];

var site = metadata.deviceName.split('_')[0];

// var ts = parseLong(metadata.ts);

var msgData = {
  site: site,
  siteCode: siteCode,
  systemCode: systemCode,
  workshopNo: workshopNo,
  deviceCode: deviceCode,
  payload: payloadObj,
  topic: msg.topic,
  clientid: msg.clientid,
  username: msg.username,
  peername: msg.peername,
  ts: msg.timestamp,
  tbMetadata: metadata,
};

return {
  msg: msgData,
  metadata: metadata,
  msgType: msgType,
};
